import PageTitle from "@/components/page-title";
import AppError from "@/shared/app-error";
import { Post } from "@prisma/client";
import Link from "next/link";
import { notFound } from "next/navigation";
import Markdown from "react-markdown";
import { getPostById } from "./actions";

export default async function Page({ params }: { params: { postId: string } }) {
  const result = await getPostById(params.postId);
  if (result.ok) {
    const post = result.data;
    if (post) {
      return <MarkdownPost post={post} />;
    } else {
      notFound();
    }
  } else {
    throw new AppError({ code: result.code, message: result.message });
  }
}

function MarkdownPost({ post }: { post: Post }) {
  return (
    <div>
      <PageTitle title={post.title} />
      <Link href={`/posts/${post.id}/update`}>修改 TODO: 只能修改自己的</Link>
      <Markdown className="prose">{post.content}</Markdown>
    </div>
  );
}
